<template>
  <div class="container">
    <a-card>
      <a-card-meta title="退货地址">
        <template #description
          >配置您的退货地址，所有售后商品将会寄回至您的退货地址处。</template
        >
      </a-card-meta>
      <template v-if="dataSource.length > 0">
        <div
          v-for="item in dataSource"
          :key="item.id"
          class="card-content flex"
        >
          <div>
            <p>
              <img src="/image/icon1.png" alt="" srcset="" />
              <span>收件人：{{ item.contactUserName }}</span>
            </p>
            <p>
              <img src="/image/icon2.png" alt="" srcset="" />
              <span>联系电话：{{ item.contactMobile }}</span>
            </p>
            <p>
              <img src="/image/icon3.png" alt="" srcset="" />
              <span>退货地址：{{ item.cityStr }}{{ item.addressDetail }}</span>
            </p>
          </div>
          <a-button type="primary" @click="handleEdit(item)">编辑</a-button>
        </div>
      </template>
      <div v-else class="empty">
        <img src="/image/icon4.png" alt="" srcset="" />
        <p>暂未添加退货地址</p>
        <a-button type="primary" @click="handleAddAddress">添加地址</a-button>
      </div>
    </a-card>
    <addressModal
      v-if="show"
      :show="show"
      :title="title"
      :formState="formState"
      @handleSave="handleSave"
      @handleCancel="handleCancel"
    />
  </div>
</template>
<script setup>
import addressModal from "./components/addressModal.vue";
import {
  queryUserAddressList,
  editOrSaveUserAddress,
  deleteUserAddress,
} from "@/api/config";
import { ref, onMounted } from "vue";
const empty = ref(false);
const show = ref(false);
const title = ref("");
const formState = ref({});
function handleAddAddress() {
  show.value = true;
  formState.value = {};
  title.value = "添加退货地址";
}
function handleEdit(item) {
  formState.value = JSON.parse(JSON.stringify(item));
  show.value = true;
  title.value = "修改退货地址";
}
function handleSave() {
  show.value = false;
  queryUserAddressListFn();
}
function handleCancel() {
  show.value = false;
}
const dataSource = ref([]);
const queryUserAddressListFn = async () => {
  const result = await queryUserAddressList({ page: 1, pageSize: 1 });
  if (result.code === 200) {
    dataSource.value = result.data.list;
  }
};
onMounted(() => {
  queryUserAddressListFn();
});
</script>
<style lang="less" scoped>
.flex {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card-content {
  padding: 24px;
  background-color: #f3f5f7;
  border-radius: 8px;
  margin-top: 24px;
  p {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    img {
      width: 20px;
      height: 20px;
    }
    span {
      font-size: 12px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #2b3453;
      line-height: 14px;
      margin-left: 8px;
    }
  }
  p:last-child {
    margin-bottom: 0;
  }
}
.empty {
  margin-top: 24px;
  background: #f3f5f7;
  border-radius: 8px;
  padding: 40px;
  text-align: center;
  img {
    width: 56px;
  }
  p {
    font-size: 14px;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: 500;
    color: #262e47;
    line-height: 16px;
    margin: 24px 0;
  }
}
</style>
